{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b6129f36-526b-4876-a85e-9e0b9e9f9173",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 7 3]\n",
      " [2 8 5]\n",
      " [9 1 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "n=np.array([[4,7,3],[2,8,5],[9,1,6]])\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85647277-b584-4600-bbf2-57478f6d39b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组排序 [[3 4 7]\n",
      " [2 5 8]\n",
      " [1 6 9]]\n"
     ]
    }
   ],
   "source": [
    "print('数组排序', np.sort(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "21493e86-abdd-47e9-848a-7ebf822d0a47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 1 3]\n",
      " [4 7 5]\n",
      " [9 8 6]]\n"
     ]
    }
   ],
   "source": [
    "# 按行排序\n",
    "print(np.sort(n,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5d3de628-9e28-435d-b88a-98cb7d2d6645",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 4 7]\n",
      " [2 5 8]\n",
      " [1 6 9]]\n"
     ]
    }
   ],
   "source": [
    "# 按列排序\n",
    "print(np.sort(n, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "78dcb6c1-9707-430c-a212-53676be6ca9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 4 2 3 1 6 7 0]\n",
      "[1 2 3 4 5 6 8 9]\n"
     ]
    }
   ],
   "source": [
    "x=np.array([9,5,3,4,2,1,6,8])\n",
    "y=np.argsort(x)\n",
    "print(y)\n",
    "# 排序后重构数组\n",
    "print(x[y])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0b3d564c-481b-4609-983c-5e7f86062c3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5 3 2 0 1]\n",
      "[[np.int64(615), np.int64(118), np.int64(98)], [np.int64(615), np.int64(118), np.int64(109)], [np.int64(620), np.int64(108), np.int64(108)], [np.int64(620), np.int64(115), np.int64(118)], [np.int64(621), np.int64(101), np.int64(117)], [np.int64(623), np.int64(109), np.int64(105)]]\n"
     ]
    }
   ],
   "source": [
    "# lexsort排序\n",
    "math=np.array([101,109,115,108,118,118])  # 数学\n",
    "en=np.array([117,105,118,108,98,109])  # 英语\n",
    "total=np.array([621,623,620,620,615,615])  # 总成绩\n",
    "## 排序\n",
    "sort_total=np.lexsort((en,math,total)) # 排序顺序 总--》数学-->英语\n",
    "print(sort_total) # 排序后的索引值\n",
    "lst = [[total[i], math[i], en[i]] for i in sort_total]\n",
    "n=np.array(lst)\n",
    "print(lst)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
